﻿using System.Collections.Generic;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.UI.Item;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Panel
{
    public class ExerciseDetailPanel:UIBase<ExerciseDetailPanel>
    {

        public Button ExitButton;
        public Button LeftButton;
        public Button RightButton;
        public QueItem QueItem;
        public Scrollbar Scrollbar;
        public static Transform FullScreenParent;

        private int index { get; set; }

        protected override UIType UIType
        {
            get { return UIType.Panel;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform, true);              
            ExitButton.onClick.AddListener(Exit);
            LeftButton.onClick.AddListener(() => UpdataQueInfo(-1));
            RightButton.onClick.AddListener(() => UpdataQueInfo(1));
        }

        protected override void InitOnDestroy()
        {
            FullScreenParent = null;
        }

        public void Init(QueInfo info,int index)
        {
            this.index = index;
            if (index == 0)
            {
                //隐藏左边的按钮
                LeftButton.gameObject.SetActive(false);
            }
            else
            {
                LeftButton.gameObject.SetActive(true);
            }
            if (CollectPanel._Instance.NowCourseQueInfos.Count - 1 == index)
            {
                //隐藏右边的按钮
                RightButton.gameObject.SetActive(false);
            }
            else
            {
                RightButton.gameObject.SetActive(true);
            }
            List<AnsInfo> ansInfos = NetReaderFactory.Instance().GetInfoByType<List<AnsInfo>>() as List<AnsInfo>;
            QueItem.Init(index+1,info,null,ExamType.Null, true,GetAnsInfoById(info,ansInfos));
        }

        private List<AnsInfo> GetAnsInfoById(QueInfo info ,List<AnsInfo> ansInfos ) 
        {
            if (info.ansdata != null)
            {
                return info.ansdata;
            }
            info.ansdata=new List<AnsInfo>();
            for (int i = 0; i < ansInfos.Count; i++)
            {
                if (ansInfos[i].queid == info.id)
                {
                    info.ansdata.Add(ansInfos[i]);
                }
            }
            return info.ansdata;
        }


        /// <summary>
        /// 刷新问题数据
        /// </summary>
        private void UpdataQueInfo(int i)
        {
            Scrollbar.value = 1;
            index += i;
            QueItem.Clear();
            if (CollectPanel._Instance != null)
            {
                QueInfo info = CollectPanel._Instance.NowCourseQueInfos[index];
                Init(info, index);  
            }
        }
    }
}